﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZetaPrints.ZetaClient.Presentation
{
    /// <summary>
    /// Class to sort the columns on the OrderHeader
    /// </summary>
    public class ListViewOrderHeaderSorter : IComparer
    {
        #region private attributes
        /// <summary>
        /// The column index of the item been sorted
        /// </summary>
        private int _columnIndex;
        /// <summary>
        /// numberic value assign possitive or negative for inverting the sorting
        /// </summary>
        private int _invert;
        #endregion


        #region constructors
        public ListViewOrderHeaderSorter(int columnIndex, bool invert)
        {
            _columnIndex = columnIndex;

            if (invert)
            {
                _invert = -1;
            }
            else
            {
                _invert = 1;
            }
        }
        #endregion


        #region public methods
        /// <summary>
        /// To compare the two entries to sort them
        /// </summary>
        /// <param name="listViewItem1Obj"></param>
        /// <param name="listViewItem2Obj"></param>
        /// <returns></returns>
        public int Compare(object listViewItem1Obj, object listViewItem2Obj)
        {
            ListViewItem listViewItem1 = listViewItem1Obj as ListViewItem;
            ListViewItem listViewItem2 = listViewItem2Obj as ListViewItem;

            int compareResult = 0;

            /// date time in case this is a date time
            DateTime dateTime1;

            /// the number entry
            int numeric1;

            /// are we date time?
            if (DateTime.TryParse(listViewItem1.SubItems[_columnIndex].Text,out dateTime1 ))
            {
                DateTime dateTime2;
                if (DateTime.TryParse(listViewItem2.SubItems[_columnIndex].Text, out dateTime2))
                {
                    if (dateTime1.Ticks > dateTime2.Ticks)
                    {
                        compareResult = 1;
                    }
                    else
                    {
                        compareResult = -1;
                    }
                }
                else
                {
                    compareResult = 1;
                }
            }
            else if (int.TryParse(listViewItem1.SubItems[_columnIndex].Text, out numeric1))
            {
                /// we are numeric
                int numeric2;
                if (int.TryParse(listViewItem2.SubItems[_columnIndex].Text, out numeric2))
                {
                    if (numeric1 > numeric2)
                    {
                        compareResult = 1;
                    }
                    else
                    {
                        compareResult = -1;
                    }
                }
                else
                {
                    compareResult = 1;
                }
            }
            else
            {
                compareResult = listViewItem1.SubItems[_columnIndex].Text.CompareTo(listViewItem2.SubItems[_columnIndex].Text);
            }

            return (_invert * compareResult);
        }
       #endregion
    }
}
